package io.renren.modules.udp.controller;

import java.lang.reflect.Field;
import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.PublicUtil;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.app.service.UserService;
import io.renren.modules.udp.entity.R5rmObjectsEntity;
import io.renren.modules.udp.service.R5rmObjectsService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.udp.entity.R5rmSkillstatusEntity;
import io.renren.modules.udp.service.R5rmSkillstatusService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.common.utils.ShiroUtils.getUserEntity;


/**
 * 技术状态
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-01-13 15:57:13
 */
@RestController
@RequestMapping("udp/r5rmskillstatus")
public class R5rmSkillstatusController {
    @Autowired
    private R5rmSkillstatusService r5rmSkillstatusService;
    @Autowired
    private R5rmObjectsService r5rmObjectsService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("udp:r5rmskillstatus:list")
    public R list(@RequestParam Map<String, Object> params){
       // List<R5rmSkillstatusEntity> list = new ArrayList<>();
         //R5rmSkillstatusEntity r5rmSkillstatusEntity = new R5rmSkillstatusEntity();
        //List<Field> fields = PublicUtil.getFieldOfClass(r5rmSkillstatusEntity.getClass());
/*        for (int j = 0, size = fields.size(); j < size; j++) {
            try {
                // 获取属性的名字
                Field each = fields.get(j);
                String name = each.getName();
                each.setAccessible(true);
                each.set(r5rmSkillstatusEntity, redisUtils.get(name));
            } catch (Exception e) {
                R.error("执行异常");
                e.printStackTrace();
            }
        }*/
       // list.add(r5rmSkillstatusEntity);
        PageUtils page = r5rmSkillstatusService.queryPage(params);
       // page.setList(list);
        return R.ok().put("page", page);
    }

    @RequestMapping("/redisList/{ssObj}")
    @RequiresPermissions("udp:r5rmskillstatus:redisList")
    public R redisList( @PathVariable("ssObj") String ssObj) {
        R5rmSkillstatusEntity r5rmSkillstatusEntity = new R5rmSkillstatusEntity();
        String str = redisUtils.get(ssObj);
        if (!StringUtils.isEmpty(str)) {
            r5rmSkillstatusEntity = r5rmSkillstatusService.getUdp(str);
        }
        /*List<Field> fields = PublicUtil.getFieldOfClass(r5rmSkillstatusEntity.getClass());
        for (int j = 0, size = fields.size(); j < size; j++) {
            try {
                // 获取属性的名字
                Field each = fields.get(j);
                String name = each.getName();
                each.setAccessible(true);
                each.set(r5rmSkillstatusEntity, );
            } catch (Exception e) {
                R.error("执行异常");
                e.printStackTrace();
            }
        }*/
        return R.ok().put("model",r5rmSkillstatusEntity);
    }

    @RequestMapping("/home")
    @RequiresPermissions("udp:r5rmskillstatus:home")
    public R home(){
        //定义接收数据
        JSONObject result = new JSONObject();
        //获取当前登录用户
        String username = getUserEntity().getUsername();
        //获取光发电量与风发电量
        Map dlMap = r5rmSkillstatusService.getDlMap(username);
        Map objMap = new HashMap();
        objMap.put("username",username);
        //运行设备
        objMap.put("obj_udfchar04","1");
        int yunxing = r5rmSkillstatusService.selectCountByUser(objMap);
        //待机设备
        objMap.put("obj_udfchar04","0");
        int daiji = r5rmSkillstatusService.selectCountByUser(objMap);
        //故障设备
        objMap.put("obj_udfchar04","2");
        int guzhang = r5rmSkillstatusService.selectCountByUser(objMap);

        //存放数据
        result.put("ssUdfnumber01",dlMap.get("ssUdfnumber01"));
        result.put("ssUdfnumber02",dlMap.get("ssUdfnumber02"));
        result.put("yunxing",yunxing);
        result.put("daiji",daiji);
        result.put("guzhang",guzhang);
        return R.ok().put("result",result);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{ssId}")
    @RequiresPermissions("udp:r5rmskillstatus:info")
    public R info(@PathVariable("ssId") Long ssId){
		R5rmSkillstatusEntity r5rmSkillstatus = r5rmSkillstatusService.selectById(ssId);

        return R.ok().put("r5rmSkillstatus", r5rmSkillstatus);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("udp:r5rmskillstatus:save")
    public R save(@RequestBody R5rmSkillstatusEntity r5rmSkillstatus){
		r5rmSkillstatusService.insert(r5rmSkillstatus);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("udp:r5rmskillstatus:update")
    public R update(@RequestBody R5rmSkillstatusEntity r5rmSkillstatus){
		r5rmSkillstatusService.updateById(r5rmSkillstatus);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("udp:r5rmskillstatus:delete")
    public R delete(@RequestBody Long[] ssIds){
		r5rmSkillstatusService.deleteBatchIds(Arrays.asList(ssIds));

        return R.ok();
    }

}
